let layer = layui.layer
let query = {
  pagenum: 1,  //页码值
  pagesize: 10,  //每页显示多少条数据
  cateid: localStorage.getItem("id")
}

getNew()
function getNew(){
    axios.get('/index/list',{params:{
    page: query.pagenum,
    limit: query.pagesize,
    cateid: query.cateid}}).then(({data:{data:{list}}}) => {
        // console.log(list)
        const { data, count, pageSize } = list
        const rewen = data.map(({id, title, author, create_date, description, click, lovenum, pic }) => {
            const time = create_date
            const date = new Date(time)
            let newTime = date.getFullYear() + '-' + date.getMonth() + '-' + date.getDay()
            return `
              <article class="excerpt excerpt-one">
              <header>
                <a class="cat label label-important" href="">热门<i class="label-arrow"></i></a>
                <h2>
                  <a target="_blank" href="show.html?${id}" id="show" data-id="${id}" title="${title}">${title}</a>
                </h2>
              </header>
              <p class="text-muted time">
                <a href="">${author}</a> 发布于 ${newTime}
              </p>
              <p class="focus">
                <a target="_blank" href="http://blog.weknow.cn/474.shtml" class="thumbnail">
                  <span class="item"><span class="thumb-span"><img src="http://124.223.14.236:8060/${pic}"
                        class="thumb" /></span></span></a>
              </p>
              <p class="note">${description}</p>
              <p class="text-muted views">
                <span class="post-views">阅读(${click})</span>
                <span class="post-comments">评论(0)</span>
                <a href="javascript:;" class="post-like">
                  <i class="fa fa-thumbs-up"></i>赞 (<span>${lovenum}</span>)
                </a>
              </p>
            </article>`
          }).join('')
          $('.excerpt').html(rewen)
          $("#count").text(`共${count}条数据, ${Math.ceil(count / pageSize)}页`)
          pageRander(count)
    })
}

$(".excerpt").on("click", "#show", function(e) {
    localStorage.setItem("ider", $(this).attr("data-id") )
  })
  $(".content").on("click", "#show", function(){
    localStorage.setItem("ider", $(this).attr("data-id"))
  })
$('.cat-leader h1').text(localStorage.getItem('catename'))
  function pageRander(count) {
    layui.use('laypage', function () {
        let laypage = layui.laypage;
    
        //执行一个laypage实例
        laypage.render({
          elem: 'clicking', //注意，这里的 test1 是 ID，不用加 # 号 
          count,//数据总数，从服务端得到
          limit: 10,
          groups: 2,
          curr: query.pagenum,
          layout: ['prev', 'page', 'next'],
          theme: "#ff4500",
          jump: function (obj, first) {
            //obj包含了当前分页的所有参数，比如：
            // console.log(obj.curr); //得到当前页，以便向服务端请求对应页的数据。
            // console.log(obj.limit); //得到每页显示的条数
    
            // 将页码值修改为点击时的当前页
            query.pagenum = obj.curr
            // 将每页显示数据修改为点击时的选择
            query.pagesize = obj.limit
            //首次不执行
            // console.log(123)
            if (!first) {
              // 点击分页的时候发ajax请求
              getNew()
            }
          }
        })
      });
  }

